package org.dromara.business.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import cn.hutool.core.util.StrUtil;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.dromara.business.domain.bo.AnswerBo;
import org.dromara.business.domain.vo.AnswerVo;
import org.dromara.business.domain.Answer;
import org.dromara.business.mapper.AnswerMapper;
import org.dromara.business.service.AnswerService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 客服问答Service业务层处理
 *
 * @author zlj
 * @date 2024-08-28
 */
@RequiredArgsConstructor
@Service
public class AnswerServiceImpl extends ServiceImpl<AnswerMapper,Answer> implements AnswerService {

    private final AnswerMapper baseMapper;

    /**
     * 查询客服问答
     */
    @Override
    public AnswerVo queryById(Long answerId){
        return baseMapper.selectVoById(answerId);
    }

    /**
     * 查询客服问答列表
     */
    @Override
    public TableDataInfo<AnswerVo> queryPageList(AnswerBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Answer> lqw = buildQueryWrapper(bo);
        Page<AnswerVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询客服问答列表
     */
    @Override
    public List<AnswerVo> queryList(AnswerBo bo) {
        LambdaQueryWrapper<Answer> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Answer> buildQueryWrapper(AnswerBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Answer> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getAnswerKeyWord()), Answer::getAnswerKeyWord, bo.getAnswerKeyWord());
        lqw.eq(StrUtil.isNotBlank(bo.getAnswerTemplates()), Answer::getAnswerTemplates, bo.getAnswerTemplates());
        lqw.orderByDesc(Answer::getCreateTime);
        return lqw;
    }

    /**
     * 新增客服问答
     */
    @Override
    public Boolean insertByBo(AnswerBo bo) {
        Answer add = MapstructUtils.convert(bo, Answer.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setAnswerId(add.getAnswerId());
        }
        return flag;
    }

    /**
     * 修改客服问答
     */
    @Override
    public Boolean updateByBo(AnswerBo bo) {
        Answer update = MapstructUtils.convert(bo, Answer.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Answer entity){

    }

    /**
     * 批量删除客服问答
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){

        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
